﻿using BITUI;
using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace app.audioplayer
{
    class Program
    {
        static OSPlatform GetPlatform()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
                return OSPlatform.FreeBSD;
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                return OSPlatform.Linux; 
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                return OSPlatform.OSX;
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                return OSPlatform.Windows;
            throw new Exception("not support.");


        }
        static void Main(string[] args)
        {
            OSPlatform p = GetPlatform();
            Console.WriteLine("Hello World!"+p);

            //set currpath to exe
            var exepath = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);
            var curpath = System.IO.Directory.GetCurrentDirectory();

            System.Console.WriteLine("curpath =" + curpath);
            System.Console.WriteLine("exepath =" + exepath);

            System.IO.Directory.SetCurrentDirectory(exepath);



            Window mainWindow = OpenWindow1();

            BITUI.AppFramework.MainLoop(mainWindow);
        }

        static Window OpenWindow1()
        {
            var option = WindowOption.Auto_Scale4X;
            option.title = "helloworld";
            option.tileWidth = 45;
            option.tileHeight = 50;
            option.linear = false;
            option.scale = 2.0F;
            option.scaleMode = WindowOption_ScaleMode.AutoTileCount;

            var win = BITUI.AppFramework.CreateWindow(option);

            //Bank是存放Tile的集合，一个Tile就是8x8像素的原始图块，Bank里的Tile是任意尺寸的（1~255不需要方形）
            //但是他们绘制到屏幕上时要8x8对齐
            //所以Bank既是图集，又是字库
            BITUI.Tile.TileTool.LoadASCII12toBank(win.GetBank(0)); //英文12点阵字库 ，8字库太小了
            BITUI.Tile.TileTool.LoadHZK12toBank(win.GetBank(0));//中文12点阵字库，中文能显示清楚12最合适


            var tile = win.GetBank(0).CreateTile("grid", BITUI.Tile.TileTool.GenGridTileData());
            var tile2 = win.GetBank(0).CreateTile("border1", BITUI.Tile.TileTool.GenOnePixelBorder(true, true, true, true, 0x35));
            var tile3 = win.GetBank(0).CreateTile("border2", BITUI.Tile.TileTool.GenTwoPixelBorder(true, true, true, true, 0x35));

            var scale9_1 = BITUI.Tile.Scale9.CreateSingleBorderScale9(win.GetBank(0), "scale1");//单边框，代码生成的，不用图集
            var scale9_2 = BITUI.Tile.Scale9.CreateTwoBorderScale9(win.GetBank(0), "scale2");//双边框,代码生成的，不用图集


            var rootSplit = new BITUI.Element.SplitPanel();
            rootSplit.Fill = true;
            rootSplit.TileYLocal = 2;
            rootSplit.TileXLocal = 1;
            rootSplit.BorderRightLocal = 1;
            rootSplit.BorderBottomLocal = 1;
            win.RootElement = new BITUI.Element.Canvas();
            win.RootElement.AddChild(rootSplit);

            var labelfps = new BITUI.Element.Label(win.GetBank(0), "fps=0") { TileXLocal = 0, TileYLocal = 0 };
            win.RootElement.AddChild(labelfps);

            var canvasPanel = rootSplit.panelA;
            canvasPanel.backGround = scale9_2;
            canvasPanel.backGroundColorReplace = 35;
            canvasPanel.clipBorderPixel = 4;

            rootSplit.panelB.backGround = scale9_2;
            rootSplit.panelB.backGroundColorReplace = 37;
            rootSplit.panelB.clipBorderPixel = 4;


            canvasPanel.AddChild(new BITUI.Element.Label(win.GetBank(0), "Audio Player!") { TileXLocal = 4, TileYLocal = 3 });


            var files1 = System.IO.Directory.GetFiles("./", "*.ogg");
            var files2 = System.IO.Directory.GetFiles("./", "*.mp3");
            var files = files1.Union(files2);
            int y = 10;
            foreach (var f in files)
            {
                var btn = new BITUI.Element.Button();
                btn.TileXLocal = 4;
                btn.TileYLocal = y;
                y += 3;
                var playfile = f;
                btn.stateNormal = new BITUI.Element.Label(win.GetBank(0), playfile) { TileWidth = 15, TileHeight = 2, backGround = scale9_1, backGroundColorReplace = 0x38 };
                btn.statePointOver = new BITUI.Element.Label(win.GetBank(0), playfile) { TileWidth = 15, TileHeight = 2, backGround = scale9_1, backGroundColorReplace = 0x30 };
                btn.statePress = new BITUI.Element.Label(win.GetBank(0), playfile) { TileWidth = 15, TileHeight = 2, backGround = tile, backGroundColorReplace = 0x30 };
                canvasPanel.AddChild(btn);

                btn.OnClick += () =>
                {
                    if (BITUI.AppFramework.AudioSystem.AudioInited)
                    {
                        BITUI.AppFramework.AudioSystem.PlayBackgroundMusic(playfile);
                    }
                };
            }



            win.eventOnUpdate += () =>
            {
                labelfps.text = "fps=" + AppFramework.Fps.ToString("F2");
            };


            return win;
        }
    }
}
